<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <title>注册</title>
    <link href="../css/register.css" rel="stylesheet">
    <script src="../js/jquery.js"></script>
    <style>
        span {
            color: red;
            font-size: 12px;
        }
    </style>
</head>

<body class="bg-gradient-primary">

<div class="container">

    <div class="card o-hidden border-0 shadow-lg my-5">
        <div class="card-body p-0">
            <!-- Nested Row within Card Body -->
            <div class="row">
                <div class="col-lg-5 d-none d-lg-block bg-register-image"></div>
                <div class="col-lg-7">
                    <div class="p-5">
                        <div class="text-center">
                            <h1 class="h4 text-gray-900 mb-4">个人信息</h1>
                        </div>
                        <form class="user">
                            <div class="form-group">
                                <span>*</span>
                                学号/工号/账号:
                                <label for="userid"></label>
                                <input type="text"
                                       class="form-control form-control-user"
                                       id="userid"
                                       name="userid" placeholder="学号/工号/账号" readonly>
                                <span>*不可随意修改</span>
                            </div>
                            <div class="form-group">
                                <span>*</span>
                                姓名:
                                <label for="username"></label>
                                <input type="text"
                                       class="form-control form-control-user"
                                       id="username"
                                       name="username" placeholder="用户名">
                            </div>
                            <div class="form-group">
                                <span>*</span>
                                密码:
                                <label for="password"></label>
                                <input type="text"
                                       class="form-control form-control-user"
                                       id="password"
                                       name="password" placeholder="密码">
                            </div>
                            <div class="form-group">
                                <span>*</span>
                                职务:
                                <label for="user_priority"></label>
                                <input id="user_priority" class="form-control form-control-user" readonly>
                                <span>*不可随意修改</span>
                                <!--                                <select id="user_priority">-->
                                <!--                                    <option value="0" selected>学生</option>-->
                                <!--                                    <option value="1">教师</option>-->
                                <!--                                </select>-->
                            </div>
                            <div class="form-group">
                                <span>*</span>
                                性别:
                                <label>
                                    <input type="radio" name="sex" id="sex1" value="男" checked>男
                                </label>
                                <label>
                                    <input type="radio" name="sex" id="sex2" value="女">女
                                </label>
                            </div>
                            <div class="form-group">
                                <span>*</span>
                                院系:
                                <label for="faculty"></label>
                                <select id="faculty">
                                    <option selected>计算机科学与技术</option>
                                    <option>经济学院</option>
                                    <option>物理学院</option>
                                    <option>土木工程</option>
                                </select>
                            </div>
                            <font color="red"></font>
                            <button type="button" class="btn btn-primary btn-user btn-block"
                                    onclick="update()">修改
                            </button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    $(function () {
        $.ajax({
            type: "post",
            url: "http://localhost:8080/user/getCurrentUser",
            success: function (result) {
                let res = result.object;
                if (result.code == '200') {
                    document.getElementById("userid").value = res.userid;
                    document.getElementById("username").value = res.username;
                    document.getElementById("password").value = res.password;
                    document.getElementById("faculty").value = res.faculty;
                    if (res.sex == '女') {
                        $("#sex1").prop("checked", false);
                        $("#sex2").prop("checked", true);
                    }
                    if (res.priority == "0") {
                        document.getElementById("user_priority").value = "学生";
                    } else if (res.priority == "1") {
                        document.getElementById("user_priority").value = "教师";
                    } else if (res.priority == "2") {
                        document.getElementById("user_priority").value = "管理员";
                    }
                } else {
                    alert("请先登录账号！");
                    window.location.href = "login.html";
                }
            }
        });
    });

    function update() {
        let userid = $("#userid").val();
        let username = $("#username").val();
        let pwd = $("#password").val();
        let sex = $("input[name='sex']:checked").val();
        let faculty = $("#faculty option:selected").text();
        let priority = $("#user_priority").val();

        let data = {
            "userid": userid,
            "username": username,
            "password": pwd,
            "sex": sex,
            "faculty": faculty,
            "priority": priority
        };
        $.post('http://localhost:8080/user/updateUserInfo', data, function (result) {
            if (result.code == 200) {
                alert(result.msg);
                location.replace(location.href)
            } else {
                alert(result.msg);
            }
        })
    }
</script>
</body>
</html>